package com.gzc.just.play.last.war.sceneserver.gameobject.unit;

import com.gzc.just.play.last.war.sceneserver.gameobject.base.GameObjectCamp;
import com.gzc.just.play.last.war.sceneserver.gameobject.base.GameObjectType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * NPC类 - 非玩家控制的单位
 * 
 * 对应C++ CSSNPC
 * 包括野怪、防御塔、建筑等NPC
 * 
 * @author AI Assistant
 * @version 1.0.0
 * @since 2025-11-22
 */
public abstract class NPC extends GameUnit {
    
    private static final Logger log = LoggerFactory.getLogger(NPC.class);
    private static final long serialVersionUID = 1L;

    // NPC属性
    private int npcTypeId;              // NPC类型ID
    private String npcName;             // NPC名称
    
    // Getter和Setter方法
    public int getNpcTypeId() {
        return npcTypeId;
    }
    
    public void setNpcTypeId(int npcTypeId) {
        this.npcTypeId = npcTypeId;
    }
    
    public String getNpcName() {
        return npcName;
    }
    
    public void setNpcName(String npcName) {
        this.npcName = npcName;
    }
    
    // AI相关
    private boolean hasAI;              // 是否拥有AI
    private int aiState;                // AI状态
    
    // 掉落相关
    private int dropGoldMin;            // 掉落金币最小值
    private int dropGoldMax;            // 掉落金币最大值
    private int dropExpMin;             // 掉落经验最小值
    private int dropExpMax;             // 掉落经验最大值

    /**
     * 构造函数
     */
    public NPC(long objectId, int npcTypeId, String npcName, GameObjectCamp camp,
               int maxHealth, int maxMana, double attack, double defense, double speed) {
        super(objectId, GameObjectType.NPC, camp, maxHealth, maxMana, attack, defense, speed);
        
        this.npcTypeId = npcTypeId;
        this.npcName = npcName;
        this.hasAI = false;
        this.aiState = 0;
        
        this.dropGoldMin = 0;
        this.dropGoldMax = 0;
        this.dropExpMin = 0;
        this.dropExpMax = 0;
    }

    /**
     * 初始化NPC
     */
    @Override
    public void initialize() {
        super.initialize();
        log.info("初始化NPC: npcId={}, npcName={}, camp={}", npcTypeId, npcName, getCamp());
    }

    /**
     * 销毁NPC
     */
    @Override
    public void destroy() {
        log.info("销毁NPC: npcId={}, npcName={}", npcTypeId, npcName);
        super.destroy();
    }
    
    /**
     * 获取是否拥有AI
     */
    public boolean isHasAI() {
        return hasAI;
    }
    
    /**
     * 获取AI状态
     */
    public int getAiState() {
        return aiState;
    }
    
    /**
     * 设置AI状态
     */
    public void setAiState(int aiState) {
        this.aiState = aiState;
    }
    
    /**
     * 获取掉落金币最小值
     */
    public int getDropGoldMin() {
        return dropGoldMin;
    }
    
    /**
     * 设置掉落金币最小值
     */
    public void setDropGoldMin(int dropGoldMin) {
        this.dropGoldMin = dropGoldMin;
    }
    
    /**
     * 获取掉落金币最大值
     */
    public int getDropGoldMax() {
        return dropGoldMax;
    }
    
    /**
     * 设置掉落金币最大值
     */
    public void setDropGoldMax(int dropGoldMax) {
        this.dropGoldMax = dropGoldMax;
    }
    
    /**
     * 获取掉落经验最小值
     */
    public int getDropExpMin() {
        return dropExpMin;
    }
    
    /**
     * 设置掉落经验最小值
     */
    public void setDropExpMin(int dropExpMin) {
        this.dropExpMin = dropExpMin;
    }
    
    /**
     * 获取掉落经验最大值
     */
    public int getDropExpMax() {
        return dropExpMax;
    }
    
    /**
     * 设置掉落经验最大值
     */
    public void setDropExpMax(int dropExpMax) {
        this.dropExpMax = dropExpMax;
    }

    /**
     * 处理心跳
     */
    @Override
    public void processHeartbeat(long currentTime, long deltaTime) {
        if (!isAlive()) {
            return;
        }

        super.processHeartbeat(currentTime, deltaTime);
        
        // NPC特定逻辑
        if (hasAI) {
            processAI(currentTime, deltaTime);
        }
    }

    /**
     * 处理AI逻辑
     */
    protected abstract void processAI(long currentTime, long deltaTime);

    /**
     * 检查死亡状态
     */
    @Override
    protected void checkDeadState() {
        if (getCurrentHealth() <= 0 && !isDead()) {
            die();
            onDeath();
        }
    }

    /**
     * NPC死亡时的特殊处理
     */
    protected void onDeath() {
        log.info("NPC {} 已死亡: npcId={}, npcName={}", getObjectId(), npcTypeId, npcName);
    }

    /**
     * 获取掉落的金币
     */
    public int getDropGold() {
        if (dropGoldMin >= dropGoldMax) {
            return dropGoldMin;
        }
        int range = dropGoldMax - dropGoldMin;
        return dropGoldMin + (int)(Math.random() * range);
    }

    /**
     * 获取掉落的经验
     */
    public int getDropExp() {
        if (dropExpMin >= dropExpMax) {
            return dropExpMin;
        }
        int range = dropExpMax - dropExpMin;
        return dropExpMin + (int)(Math.random() * range);
    }

    /**
     * 设置掉落金币范围
     */
    public void setDropGoldRange(int min, int max) {
        this.dropGoldMin = min;
        this.dropGoldMax = Math.max(min, max);
    }

    /**
     * 设置掉落经验范围
     */
    public void setDropExpRange(int min, int max) {
        this.dropExpMin = min;
        this.dropExpMax = Math.max(min, max);
    }

    /**
     * 是否为NPC
     */
    @Override
    public boolean isNPC() {
        return true;
    }

    /**
     * 启用AI
     */
    public void enableAI() {
        this.hasAI = true;
        log.info("NPC {} 已启用AI", getObjectId());
    }

    /**
     * 禁用AI
     */
    public void disableAI() {
        this.hasAI = false;
        log.info("NPC {} 已禁用AI", getObjectId());
    }

    @Override
    public String toString() {
        return String.format("NPC{id=%d, npcId=%d, npcName=%s, camp=%s, hp=%d/%d, hasAI=%b, alive=%b}",
                getObjectId(), npcTypeId, npcName, getCamp(), 
                getCurrentHealth(), getMaxHealth(), hasAI, !isDead());
    }
}
